import 'package:flutter/material.dart';

import '../../resources/colors.dart';
import '../my_text.dart';
import 'my_button_theme.dart';

/// 按钮类型枚举
/// - text: 文本按钮
/// - outlined: 轮廓按钮
/// - gray: 灰色按钮
/// - filled: 填充按钮
enum MyButtonType { text, outlined, gray, filled }

/// 按钮尺寸枚举
/// - small: 小尺寸
/// - normal: 正常尺寸
/// - large: 大尺寸
enum MyButtonSize {
  small,
  normal,
  large,
}

/// 自定义按钮组件
///
/// 提供多种类型和尺寸的按钮，支持自定义样式和行为
class MyButton extends StatelessWidget {
  /// 按钮类型
  final MyButtonType type;

  /// 按钮尺寸
  final MyButtonSize size;

  /// 点击回调函数
  final VoidCallback? onPressed;

  /// 长按回调函数
  final VoidCallback? onLongPress;

  /// 按钮图标
  final Widget? icon;

  /// 自定义子组件
  final Widget? child;

  /// 按钮文本
  final String? text;

  /// 文本颜色
  final Color? textColor;

  /// 文本大小
  final double? textSize;

  /// 文本字重
  final FontWeight? textWeight;

  /// 按钮背景颜色
  final Color? color;

  /// 按钮点击时的高亮颜色
  final Color? overlayColor;

  /// 边框颜色
  final Color? borderColor;

  /// 是否启用按钮
  final bool enable;

  /// 是否使用国际化文本
  final bool? i18n;

  /// 是否扩展按钮宽度至最大
  final bool? expanded;

  /// 按钮圆角半径
  final double? radius;

  /// 按钮内边距
  final EdgeInsetsGeometry? padding;

  /// 自定义边框样式
  final BorderSide? borderSide;

  ///文字下划线
  final bool? underline;

  /// 基础构造函数
  const MyButton({
    super.key,
    this.type = MyButtonType.filled,
    this.child,
    this.text,
    this.onPressed,
    this.textColor,
    this.textWeight,
    this.color,
    this.enable = true,
    this.i18n,
    this.icon,
    this.size = MyButtonSize.normal,
    this.radius,
    this.onLongPress,
    this.padding,
    this.overlayColor,
    this.expanded,
    this.textSize,
    this.borderSide,
    this.borderColor,
    this.underline,
  });

  /// 创建文本按钮
  factory MyButton.text({
    Key? key,
    MyButtonSize size = MyButtonSize.normal,
    required String? text,
    Widget? icon,
    Widget? child,
    VoidCallback? onPressed,
    VoidCallback? onLongPress,
    Color? textColor,
    FontWeight? textWeight,
    Color? color,
    bool enable = true,
    bool? i18n,
    double? radius,
    EdgeInsetsGeometry? padding,
  }) {
    return MyButton(
      key: key,
      size: size,
      type: MyButtonType.text,
      onPressed: onPressed,
      onLongPress: onLongPress,
      text: text,
      icon: icon,
      textColor: textColor,
      textWeight: textWeight,
      color: color,
      enable: enable,
      i18n: i18n,
      radius: radius,
      padding: padding,
      child: child,
    );
  }

  /// 创建轮廓按钮
  factory MyButton.outlined({
    Key? key,
    MyButtonSize size = MyButtonSize.normal,
    String? text,
    Widget? icon,
    Widget? child,
    VoidCallback? onPressed,
    VoidCallback? onLongPress,
    Color? textColor,
    Color? color,
    Color? borderColor,
    bool enable = true,
    bool? i18n,
    double? radius,
    EdgeInsetsGeometry? padding,
  }) {
    return MyButton(
      key: key,
      size: size,
      type: MyButtonType.outlined,
      onPressed: onPressed,
      onLongPress: onLongPress,
      text: text,
      icon: icon,
      textColor: textColor,
      color: color,
      borderColor: borderColor,
      enable: enable,
      i18n: i18n,
      radius: radius,
      padding: padding,
      child: child,
    );
  }

  /// 创建灰色按钮
  factory MyButton.gray({
    Key? key,
    MyButtonSize size = MyButtonSize.normal,
    String? text,
    Widget? icon,
    Widget? child,
    VoidCallback? onPressed,
    VoidCallback? onLongPress,
    Color? textColor,
    Color? color,
    bool enable = true,
    bool? i18n,
    double? radius,
    EdgeInsetsGeometry? padding,
  }) {
    return MyButton(
      key: key,
      size: size,
      type: MyButtonType.gray,
      onPressed: onPressed,
      onLongPress: onLongPress,
      text: text,
      icon: icon,
      textColor: textColor,
      color: color,
      enable: enable,
      i18n: i18n,
      radius: radius,
      padding: padding,
      child: child,
    );
  }

  /// 创建填充按钮
  factory MyButton.filled({
    Key? key,
    MyButtonSize size = MyButtonSize.normal,
    String? text,
    Widget? icon,
    Widget? child,
    VoidCallback? onPressed,
    VoidCallback? onLongPress,
    Color? textColor,
    Color? color,
    bool enable = true,
    bool? i18n,
    double? radius,
    EdgeInsetsGeometry? padding,
  }) {
    return MyButton(
      key: key,
      size: size,
      type: MyButtonType.filled,
      onPressed: onPressed,
      onLongPress: onLongPress,
      text: text,
      icon: icon,
      textColor: textColor,
      color: color,
      enable: enable,
      i18n: i18n,
      radius: radius,
      padding: padding,
      child: child,
    );
  }

  /// 创建大尺寸按钮
  factory MyButton.large({
    Key? key,
    MyButtonType type = MyButtonType.filled,
    String? text,
    Widget? icon,
    Widget? child,
    VoidCallback? onPressed,
    VoidCallback? onLongPress,
    Color? textColor,
    Color? color,
    bool enable = true,
    bool? i18n,
    bool expanded = true,
    double? radius,
    EdgeInsetsGeometry? padding,
    BorderSide? borderSide,
  }) {
    return MyButton(
      key: key,
      type: type,
      onPressed: onPressed,
      onLongPress: onLongPress,
      text: text,
      icon: icon,
      textColor: textColor,
      color: color,
      enable: enable,
      i18n: i18n,
      expanded: expanded,
      radius: radius,
      padding: padding,
      borderSide: borderSide,
      size: MyButtonSize.large,
      child: child,
    );
  }

  /// 创建小尺寸按钮
  factory MyButton.small({
    Key? key,
    MyButtonType type = MyButtonType.filled,
    String? text,
    Widget? icon,
    Widget? child,
    VoidCallback? onPressed,
    VoidCallback? onLongPress,
    Color? textColor,
    Color? color,
    bool enable = true,
    bool? i18n,
    double? radius,
    EdgeInsetsGeometry? padding,
  }) {
    return MyButton(
      key: key,
      type: type,
      onPressed: onPressed,
      onLongPress: onLongPress,
      text: text,
      icon: icon,
      textColor: textColor,
      color: color,
      enable: enable,
      i18n: i18n,
      radius: radius,
      padding: padding,
      size: MyButtonSize.small,
      child: child,
    );
  }

  /// 判断是否为小尺寸按钮
  bool get isSmall => size == MyButtonSize.small;

  /// 判断是否为正常尺寸按钮
  bool get isNormal => size == MyButtonSize.normal;

  /// 判断是否为大尺寸按钮
  bool get isLarge => size == MyButtonSize.large;

  /// 判断是否为文本按钮
  bool get isText => type == MyButtonType.text;

  /// 判断是否为轮廓按钮
  bool get isOutlined => type == MyButtonType.outlined;

  /// 判断是否为灰色按钮
  bool get isGray => type == MyButtonType.gray;

  /// 判断是否为填充按钮
  bool get isFilled => type == MyButtonType.filled;

  /// 判断按钮是否可用
  bool get isEnable => enable && (onPressed != null || onLongPress != null);

  /// 判断按钮是否禁用
  bool get isDisable => !isEnable;

  /// 获取文本颜色
  Color? get _textColor {
    if (isDisable) {
      return Colors.grey;
    }

    if (textColor != null) {
      return textColor;
    }

    if (isGray) {
      return Colors.grey[700];
    }

    if (isFilled) {
      return Colors.white;
    }

    if (MyButtonTheme.textColor != null) {
      return MyButtonTheme.textColor;
    }

    return JmColors.textColor;
  }

  /// 获取文本大小
  double? get _fontSize {
    if (textSize != null) {
      return textSize;
    }

    if (isSmall) {
      return 14;
    }

    if (MyButtonTheme.textSize != null) {
      return MyButtonTheme.textSize;
    }

    return null;
  }

  /// 获取文本字重
  FontWeight? get _textWeight {
    if (textWeight != null) {
      return textWeight;
    }

    if (MyButtonTheme.textWeight != null) {
      return MyButtonTheme.textWeight;
    }

    if (isLarge) {
      return FontWeight.bold;
    }

    return null;
  }

  /// 获取按钮背景颜色
  WidgetStateProperty<Color?>? get _backgroundColor {
    if (isDisable) {
      return WidgetStateProperty.all<Color?>(Colors.grey[200]);
    }

    if (color != null) {
      return WidgetStateProperty.all<Color?>(color);
    }

    if (MyButtonTheme.color != null &&
        type != MyButtonType.text &&
        type != MyButtonType.outlined) {
      return WidgetStateProperty.all<Color?>(MyButtonTheme.color);
    }

    if (isGray) {
      return WidgetStateProperty.all<Color?>(Colors.grey[200]);
    }

    if (isFilled) {
      return WidgetStateProperty.all<Color?>(JmColors.main);
    }

    return null;
  }

  /// 获取按钮点击时的高亮颜色
  WidgetStateProperty<Color?>? get _overlayColor {
    if (overlayColor != null) {
      return WidgetStateProperty.all<Color?>(overlayColor);
    }

    if (color != null) {
      return WidgetStateProperty.all<Color?>(color!.withValues(alpha: 0.8));
    }

    if (isGray) {
      return WidgetStateProperty.all<Color?>(Colors.grey[300]);
    }

    if (isFilled) {
      return WidgetStateProperty.all<Color?>(JmColors.main.withOpacity(0.8));
    }

    if (MyButtonTheme.overlayColor != null) {
      return WidgetStateProperty.all<Color?>(MyButtonTheme.overlayColor);
    }

    return null;
  }

  /// 获取按钮内边距
  WidgetStateProperty<EdgeInsetsGeometry?>? get _padding {
    if (padding != null) {
      return WidgetStateProperty.all<EdgeInsetsGeometry?>(padding);
    }

    if (isLarge) {
      return WidgetStateProperty.all<EdgeInsetsGeometry?>(
          const EdgeInsets.only(left: 16, right: 16, top: 14, bottom: 14));
    } else if (isSmall) {
      return WidgetStateProperty.all<EdgeInsetsGeometry?>(
          const EdgeInsets.only(left: 12, right: 12, top: 2, bottom: 2));
    }

    if (MyButtonTheme.padding != null) {
      return WidgetStateProperty.all<EdgeInsetsGeometry?>(
          MyButtonTheme.padding);
    }

    return null;
  }

  /// 获取按钮最小尺寸
  WidgetStateProperty<Size?>? get _minimumSize {
    if (isSmall) {
      return WidgetStateProperty.all<Size?>(const Size(60, 28));
    }

    if (isNormal) {
      return WidgetStateProperty.all<Size?>(const Size(80, 32));
    }

    if (isLarge) {
      return WidgetStateProperty.all<Size?>(const Size(120, 40));
    }

    return null;
  }

  /// 获取按钮形状
  WidgetStateProperty<OutlinedBorder?>? get _shape {
    if (radius != null) {
      return WidgetStateProperty.all<OutlinedBorder?>(
          RoundedRectangleBorder(borderRadius: BorderRadius.circular(radius!)));
    }

    if (MyButtonTheme.radius != null) {
      return WidgetStateProperty.all<OutlinedBorder?>(RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(MyButtonTheme.radius!)));
    }

    if (isLarge) {
      return WidgetStateProperty.all<OutlinedBorder?>(RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(radius ?? 12)));
    }

    if (isSmall) {
      return WidgetStateProperty.all<OutlinedBorder?>(RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(radius ?? 6)));
    }

    return null;
  }

  /// 获取按钮边框样式
  WidgetStateProperty<BorderSide?>? get _borderSide {
    if (borderSide != null) {
      return WidgetStateProperty.all<BorderSide?>(borderSide);
    }

    if (borderColor != null) {
      return WidgetStateProperty.all<BorderSide?>(
          BorderSide(color: borderColor!));
    }

    if (type == MyButtonType.outlined) {
      if (textColor != null) {
        return WidgetStateProperty.all<BorderSide?>(
            BorderSide(color: textColor!));
      }
    }

    return null;
  }

  /// 获取点击回调函数
  VoidCallback? get _onPressed {
    if (enable) {
      return onPressed;
    }
    return null;
  }

  /// 获取长按回调函数
  VoidCallback? get _onLongPress {
    if (enable) {
      return onLongPress;
    }
    return null;
  }

  /// 创建文本组件
  Widget _text() {
    return MyText(
      text ?? "",
      color: _textColor,
      size: _fontSize,
      i18n: i18n,
      weight: _textWeight,
      underline: underline,
    );
  }

  /// 创建按钮内容组件
  Widget _widget() {
    late Widget _child;
    if (child != null) {
      _child = child!;
    } else if (icon != null) {
      Widget iconWidget = icon!;

      if (icon is Icon) {
        final icon2 = icon as Icon;
        iconWidget = Icon(
          icon2.icon,
          size: icon2.size,
          color: icon2.color ?? _textColor,
          fill: icon2.fill,
          weight: icon2.weight,
          grade: icon2.grade,
        );
      }

      _child = Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          iconWidget,
          SizedBox(width: isLarge ? 8 : 4),
          Flexible(
            child: _text(),
          ),
        ],
      );
    } else {
      _child = _text();
    }

    final style = ButtonStyle(
      padding: _padding,
      minimumSize: _minimumSize,
      backgroundColor: _backgroundColor,
      shape: _shape,
      overlayColor: _overlayColor,
      side: _borderSide,

      //animationDuration: const Duration(milliseconds: 2550),
    );

    // 根据按钮类型创建对应的按钮组件
    switch (type) {
      case MyButtonType.text:
        return TextButton(
          onPressed: _onPressed,
          onLongPress: _onLongPress,
          style: style,
          child: _child,
        );
      case MyButtonType.outlined:
        return OutlinedButton(
          onPressed: _onPressed,
          onLongPress: _onLongPress,
          style: style,
          child: _child,
        );
      case MyButtonType.gray:
      case MyButtonType.filled:
        return FilledButton(
          onPressed: _onPressed,
          onLongPress: _onLongPress,
          style: style,
          child: _child,
        );
    }

    return Container();
  }

  @override
  Widget build(BuildContext context) {
    // 如果需要扩展按钮宽度，则使用SizedBox包装
    if (expanded == true) {
      return SizedBox(
        width: double.infinity,
        child: _widget(),
      );
    }
    return _widget();
  }
}
